package main

import "fmt"

type Router func(s string)

type Node struct {
	Func Router
	Next map[string]*Node
}

func (node *Node) add(word string, router Router) {

	cur := node
	for i := 0; i < len(word); i++ {
		key := string(word[i])

		if cur.Next == nil {
			cur.Next = make(map[string]*Node)
		}

		if _, ok := cur.Next[key]; !ok {
			cur.Next[key] = &Node{}

		}
		fmt.Printf("%v  %p \n", cur, &cur.Next)

		cur = cur.Next[key]
	}

	cur.Func = router

}

func (node *Node) get(word string) {

	cur := node
	for i := 0; i < len(word); i++ {
		key := string(word[i])

		if _, ok := cur.Next[key]; !ok {
			return
		}

		cur = cur.Next[key]
	}

	if cur.Func != nil {
		cur.Func(word)
	}

}

func main() {

	router := func (word string) {
		fmt.Println(word)
	}

	root := Node{}

	root.add("abc", router)
	root.add("abcd", router)

	root.get("abc")

}
